<?php

namespace addons\git\controller;

use addons\git\library\Git as G;
use addons\git\model\Git;
use think\addons\Controller;
use think\Request;

class Index extends Controller
{

    protected $config = [];

    public function __construct(Request $request = null)
    {
        $open = get_addon_config('git')['open'];
        $open = explode(',', $open);
        if (!in_array(request()->action(), $open)) {

            $this->error('未开启');
        }

        parent::__construct($request);
    }

    public function index()
    {
        if ($this->request->isGet()) {
            $this->error('404');
        }

        $msg = 'ok1';
        $status = 3;
        $type = 'unknown';

        try{
            $res = G::exec();
            $status = $res['status'];
            $msg = $res['msg'];
            $type = $res['type'];
        }catch (\Exception $e) {
            $msg = $e->getMessage();
            $status = 0;
        }

        $data = [
            'status' => $status,
            'msg' => $msg,
            'type' => $type,
            'content' => json_encode(input())
        ];

        Git::create($data);

        return 'ok';
    }

    /**
     * 当前信息
     */
    public function info()
    {
        $info = G::nowInfo();

        echo '<pre>';
        foreach ($info as $item) {
            if (trim($item)) {
                echo '<hr>';
                echo htmlspecialchars($item);
                echo '<hr>';
            }
        }
        echo '</pre>';
    }

}
